package com.whut.utils;

import com.whut.service.EquipService;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.v_dataMap.equipReportDataMap.thirdPartyRiskReport.subAssess;
import com.whut.v_model.ProblemCollectDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：xiaojun
 * @date ：Created in 2021/4/9 11:22
 * @description：处理分项评估结果
 * @modified By：
 * @version: $
 */
public class SubAssessResult {
    ReportServiceImpl reportServiceImpl;
    Map<String, Object> dataMap;
    EquipService equipService;
    RiskPoint riskPoint;

    Integer checkId;

    Double qzjPoint = 0d;
    Double sjjPoint = 0d;
    Double jsjPoint = 0d;
    Double dlPoint = 0d;

    Double part1Num = 0d;

    public void initSubAssessResult() {
        Double part1OverAllPoint = 0d;
        List<Object> p3ProblemList = new ArrayList<>();
        List<subAssess> subAssessList = new ArrayList<>();
        List<Map<String, Object>> part1List =new ArrayList<>();

        for (Integer thirdId : reportServiceImpl.getThirdSet(checkId)) {
            final List<Object> p4ProblemList = getP4ProblemList(getFourthIdList(thirdId));
            final ProblemCollectDetail firstProblemCollectDetail=getFirstProblemCollectDetail(thirdId);
            p3ProblemList.add(getP3ProblemMap( p4ProblemList,firstProblemCollectDetail));
            //统计管理行为和内业资料的分数
            final double mbPoint = getMbPoint(thirdId);
            final double  oiPoint = getOiPoint(thirdId);
            final List<Map<String, Object>> equipEvaluationList = getEquipEvaluationList(thirdId);
            final double avgEquipScore = getAvgEquipScore(equipEvaluationList);

            equipEvaluationList.add(getLastLineMap(equipEvaluationList));
            subAssessList.add(getSubAssessItem(thirdId, firstProblemCollectDetail, equipEvaluationList));
            final double overAllPoint=getQzjPoint(mbPoint, oiPoint, avgEquipScore);

            part1OverAllPoint += overAllPoint;

            part1List.add(getPart1Map(firstProblemCollectDetail, equipEvaluationList, overAllPoint));

            getPoint(firstProblemCollectDetail, overAllPoint);

            part1Num += (equipEvaluationList.size() - 1);

        }
        double part1Point = qzjPoint * 0.3 + sjjPoint * 0.3 + jsjPoint * 0.3 + dlPoint * 0.1;
        part1List.add(getPart1Map(part1List,part1OverAllPoint));

        dataMap.put("part1List", part1List);
        dataMap.put("qzjPoint", qzjPoint);
        dataMap.put("sjjPoint", sjjPoint);
        dataMap.put("jsjPoint", jsjPoint);
        dataMap.put("dlPoint", dlPoint);
        dataMap.put("part1Point", part1Point);
        dataMap.put("problemList", p3ProblemList);
        dataMap.put("subAssessList", subAssessList);
    }
    public double getQzjPoint(Double mbPoint, Double oiPoint, double avgEquipScore) {
        return (oiPoint + mbPoint + avgEquipScore) / 3;
    }
    public String getRiskLevel(double avgEquipScore) {
        String riskLevel = "";
        if (avgEquipScore >= 90) {
            riskLevel = "可忽略风险";
        } else if (avgEquipScore >= 80 && avgEquipScore < 90) {
            riskLevel = "可容忍风险";
        } else if (avgEquipScore >= 70 && avgEquipScore < 80) {
            riskLevel = "可接受风险";
        } else if (avgEquipScore >= 60 && avgEquipScore < 70) {
            riskLevel = "不可接受风险";
        } else if (avgEquipScore < 60) {
            riskLevel = "拒绝接受风险";
        }
        return riskLevel;
    }
    @SuppressWarnings("unchecked")
    private List<Map<String, Object>> getEquipEvaluationList(Integer thirdId) {
        return (List<Map<String, Object>>) equipService.getEquipscoreBycheckIdEquipType(String.valueOf(checkId), String.valueOf(thirdId));
    }

    private List<Object> getFourthIdList(Integer thirdId) {
        final List<Object> fourthIdList = reportServiceImpl.getFouthIdList(checkId, thirdId);
        return fourthIdList;
    }

    private ProblemCollectDetail getFirstProblemCollectDetail(Integer thirdId) {
        return reportServiceImpl.getPCDInfoList(checkId, thirdId).get(0);
    }

    private Map<String, Object> getPart1Map(List<Map<String, Object>> part1List,Double part1OverAllPoint ) {
        Map<String, Object> part1Map = new HashMap<>();
        part1Map.put("pName3", "总计");
        part1Map.put("overAllPoint", part1OverAllPoint / part1List.size());
        part1Map.put("num", part1Num);
        return part1Map;
    }

    private void getPoint(ProblemCollectDetail mProblemCollectDetail, double overAllPoint) {
        switch (mProblemCollectDetail.getpName3()) {
            case "塔式起重机":
                qzjPoint = overAllPoint;
                break;
            case "施工升降机":
                sjjPoint = overAllPoint;
                break;
            case "附着式升降脚手架":
                jsjPoint = overAllPoint;
                break;
            case "高处作业吊篮":
                dlPoint = overAllPoint;
                break;
            default:
                break;
        }
    }

    private Map<String, Object> getPart1Map(ProblemCollectDetail mProblemCollectDetail, List<Map<String, Object>> equipEvaluationList, double overAllPoint) {
        Map<String, Object> part1Map = new HashMap<>();
        part1Map.put("pName3", mProblemCollectDetail.getpName3());
        part1Map.put("overAllPoint", overAllPoint);
        part1Map.put("num", equipEvaluationList.size() - 1);
        return part1Map;
    }

    private subAssess getSubAssessItem(Integer thirdId, ProblemCollectDetail mProblemCollectDetail, List<Map<String, Object>> equipEvaluationList) {
        subAssess subAssessItem = new subAssess();
        subAssessItem.setpName3(mProblemCollectDetail.getpName3());
        subAssessItem.setMbPoint(getMbPoint(thirdId).toString());
        subAssessItem.setSaPoint( getAvgEquipScore(equipEvaluationList));
        subAssessItem.setOiPoint(getOiPoint(thirdId).toString());
        subAssessItem.setOverAllPoint(getQzjPoint(getMbPoint(thirdId), getOiPoint(thirdId), getAvgEquipScore(equipEvaluationList)));
        subAssessItem.setEquipEvaluationList(equipEvaluationList);
        return subAssessItem;
    }


    private Map<String, Object> getLastLineMap(List<Map<String, Object>> equipEvaluationList) {
        Map<String, Object> lastLineMap = new HashMap<>();
        final double avgEquipScore = getAvgEquipScore(equipEvaluationList);
        String riskLevel = getRiskLevel(avgEquipScore);
        lastLineMap.put("equipNum", "综合评分");
        lastLineMap.put("risklevel", riskLevel);
        lastLineMap.put("equipScore", getAvgEquipScore(equipEvaluationList));
        lastLineMap.put("risk1Num", getRiskNum(equipEvaluationList,"risk1Num"));
        lastLineMap.put("risk2Num", getRiskNum(equipEvaluationList,"risk2Num"));
        lastLineMap.put("risk3Num", getRiskNum(equipEvaluationList,"risk3Num"));
        lastLineMap.put("risk4Num", getRiskNum(equipEvaluationList,"risk4Num"));
        return lastLineMap;
    }

    private double getAvgEquipScore(List<Map<String, Object>> equipEvaluationList) {
        double avgEquipScore =0d;
        if (equipEvaluationList.size() != 0) {
            avgEquipScore = getAllEquipScore(equipEvaluationList) / equipEvaluationList.size();
        }
        return avgEquipScore;
    }

    private int getRisk2Num(List<Map<String, Object>> equipEvaluationList, String risk2Num2) {
        int risk2Num = 0;


        for (Map<String, Object> equipEvaluation : equipEvaluationList) {

            risk2Num += Integer.parseInt(equipEvaluation.get(risk2Num2).toString());

        }
        return risk2Num;
    }

    private int getRiskNum(List<Map<String, Object>> equipEvaluationList, String riskNumStr) {
        int riskNum =0;
        for (Map<String, Object> equipEvaluation : equipEvaluationList) {
            riskNum += Integer.parseInt(equipEvaluation.get(riskNumStr).toString());
        }
        return riskNum;
    }

    private double getAllEquipScore(List<Map<String, Object>> equipEvaluationList) {
        double allEquipScore =0d;
        for (Map<String, Object> equipEvaluation : equipEvaluationList) {
            allEquipScore += Double.parseDouble((String.valueOf(equipEvaluation.get("equipScore"))));
        }
        return allEquipScore;
    }

    private Double getOiPoint(Integer thirdId) {
        Double oiPoint=100d;
        for (ProblemCollectDetail problemCollectDetail : reportServiceImpl.getPCDInfoList(checkId, thirdId)) {
            //"3"是内业资料
            if ("3".equals(problemCollectDetail.getSort4())) {
                oiPoint = oiPoint - Integer.parseInt(problemCollectDetail.getReducePoint());
            }
        }
        return oiPoint;
    }

    private Double getMbPoint(Integer thirdId) {
        double mbPoint=100d;
        for (ProblemCollectDetail problemCollectDetail : reportServiceImpl.getPCDInfoList(checkId, thirdId)) {
            if (isReducePointGTPoint6(problemCollectDetail)) {
                problemCollectDetail.setReducePoint(problemCollectDetail.getPoint6());
            }
            //"2"是管理行为
            if ("2".equals(problemCollectDetail.getSort4())) {
                mbPoint = mbPoint - Integer.parseInt(problemCollectDetail.getReducePoint());
            }

        }
        return mbPoint;
    }

    private boolean isReducePointGTPoint6(ProblemCollectDetail problemCollectDetail) {
        return Integer.parseInt(problemCollectDetail.getReducePoint()) > Integer.parseInt(problemCollectDetail.getPoint6());
    }

    private Map<String, Object> getP3ProblemMap(List<Object> p4ProblemList, ProblemCollectDetail mProblemCollectDetail) {
        Map<String, Object> p3ProblemMap = new HashMap<>();
        List<Object> p3ProblemList = new ArrayList<>();
        p3ProblemMap.put("pName3", mProblemCollectDetail.getpName3());
        p3ProblemMap.put("p4ProblemList", p4ProblemList);
        return p3ProblemMap;
    }

    private List<Object> getP4ProblemList(List<Object> fourthIdList) {
        List<Object> p4ProblemList =new ArrayList<>();

        List<Map<String, Object>> divisionP6ProblemList=new ArrayList<>();
        for (Object fourthId : fourthIdList) {
            final List<ProblemCollectDetail> pcdInfoBy4thList = reportServiceImpl.getPcdInfoListBy4th(checkId, fourthId);
            final List<Map<String, Object>> p6ProblemList = reportServiceImpl.getP6ProblemList(pcdInfoBy4thList);
            //拆开checkpart和description,再按riskPoint逆序，checkPart正序
            divisionP6ProblemList = EquipCheck.divideCheckpartAndDes(p6ProblemList);
            //按riskPoint逆序
            EquipCheck.sortByRiskPointDesc(divisionP6ProblemList);
            //按checkPart逆序,如果是数字则排数字，不是数字则排首字母
            EquipCheck.sortByCheckPartDesc(divisionP6ProblemList);
            //统计riskPoint数量
            //表里的p4Name应该叫安全问题但是没改，所以只能在这里改
            p4ProblemList.add(getP4ProblemMap(pcdInfoBy4thList,divisionP6ProblemList));

        }
        riskPoint.setAllRiskPoint(riskPoint.getAllRiskPoint()+getRiskPoint(divisionP6ProblemList,0,fourthIdList));
        riskPoint.setRiskPoint1(riskPoint.getRiskPoint1()+getRiskPoint(divisionP6ProblemList,1,fourthIdList));
        riskPoint.setRiskPoint2(riskPoint.getRiskPoint2()+getRiskPoint(divisionP6ProblemList,2,fourthIdList));
        riskPoint.setRiskPoint3(riskPoint.getRiskPoint3()+getRiskPoint(divisionP6ProblemList,3,fourthIdList));
        riskPoint.setRiskPoint4(riskPoint.getRiskPoint4()+getRiskPoint(divisionP6ProblemList,4,fourthIdList));
        return p4ProblemList;
    }
    public Integer getRiskPoint(List<Map<String, Object>> divisionP6ProblemList,int index,List<Object> fourthIdList){
        int riskPoint=0;
        for (Object fourthId : fourthIdList) {
            riskPoint += EquipCheck.countRiskPoint(divisionP6ProblemList).get(index);
        }
        return riskPoint;

    }


    public SubAssessResult(ReportServiceImpl reportServiceImpl, EquipService equipService,Integer checkId,
                           RiskPoint riskPoint,Map<String, Object> dataMap ) {
        this.reportServiceImpl = reportServiceImpl;
        this.equipService=equipService;
        this.checkId=checkId;
        this.riskPoint=riskPoint;
        this.dataMap=dataMap;
    }
    public Map<String, Object> getP4ProblemMap( List<ProblemCollectDetail> pcdInfoBy4th,
                                                List<Map<String, Object>> divisionP6ProblemList) {
        Map<String, Object> p4ProblemMap = new HashMap<>();
        if (pcdInfoBy4th.get(0).getpName4().equals(pcdInfoBy4th.get(0).getpName3())) {
            p4ProblemMap.put("pName4", "安全问题");
        } else {
            p4ProblemMap.put("pName4", pcdInfoBy4th.get(0).getpName4());
        }
        p4ProblemMap.put("p6ProblemList", divisionP6ProblemList);
        return p4ProblemMap;
    }

    public SubAssessResult(ReportServiceImpl reportServiceImpl, Integer checkId) {
        this.reportServiceImpl = reportServiceImpl;
        this.checkId = checkId;
    }

    public ReportServiceImpl getReportServiceImpl() {
        return reportServiceImpl;
    }

    public void setReportServiceImpl(ReportServiceImpl reportServiceImpl) {
        this.reportServiceImpl = reportServiceImpl;
    }


    public Integer getCheckId() {
        return checkId;
    }

    public void setCheckId(Integer checkId) {
        this.checkId = checkId;
    }

    public Double getQzjPoint() {
        return qzjPoint;
    }

    public void setQzjPoint(Double qzjPoint) {
        this.qzjPoint = qzjPoint;
    }

    public Double getSjjPoint() {
        return sjjPoint;
    }

    public void setSjjPoint(Double sjjPoint) {
        this.sjjPoint = sjjPoint;
    }

    public Double getJsjPoint() {
        return jsjPoint;
    }

    public void setJsjPoint(Double jsjPoint) {
        this.jsjPoint = jsjPoint;
    }

    public Double getDlPoint() {
        return dlPoint;
    }

    public void setDlPoint(Double dlPoint) {
        this.dlPoint = dlPoint;
    }


    public Double getPart1Num() {
        return part1Num;
    }

    public void setPart1Num(Double part1Num) {
        this.part1Num = part1Num;
    }
}
